package com.jye.cloud.user.controller;

import com.jye.cloud.domain.Group;
import com.jye.cloud.domain.JsonResult;
import com.jye.cloud.user.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/group")
@Api(tags="题组控制器API")
public class GroupController {

    @Autowired
    GroupService groupService;

    @GetMapping("/findGroupById")
    @ApiOperation(value="查找题组", notes="根据id查找题组", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "题组ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findGroupById(int id){
        JsonResult jsonResult = new JsonResult();
        Group group = groupService.findGroupById(id);
        if(!StringUtils.isEmpty(group)){
            jsonResult.setData(group);
        }else{
            jsonResult.setMsg("查询失败");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findClassGroupByCId")
    @ApiOperation(value="查找题组", notes="根据cId查找题组", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "cId",
                    value = "班级ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findClassGroupByCId(int cId){
        JsonResult jsonResult = new JsonResult();
        List<Integer> ids = groupService.findClassGroupByCId(cId);
        List<Group> groupList = new ArrayList<>();
        Group group;
        for (Integer id : ids) {
            group = groupService.findGroupById(id);
            groupList.add(group);
        }
        if(groupList.size()!=0){
            jsonResult.setData(groupList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("改班级暂无测试!");
        }
        return jsonResult;
    }



}
